﻿using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using BLL.Services.Users;
using Web.Infrastructure.Models;

namespace Web.Models.Account
{
    public class EditProfileModel : BaseModel
    {
        public EditProfileModel(User user)
        {
            ToModel(user, this);
        }

        public EditProfileModel()
        {
            
        }

        [UIHint("hidden")]
        public int UserId { get; set; }

        [Display(Name = "User email")]
        [Required(ErrorMessage = @"Email is required!")]
        [StringLength(256, MinimumLength = 3)]
        [Remote("Validate", "Account", AdditionalFields = "Login,UserId", ErrorMessage = "User with the same email or login already exists.")]
        [RegularExpression(@"[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", ErrorMessage = "Please enter a valid email address.")]
        public string Email { get; set; }

        [System.ComponentModel.DataAnnotations.Compare("ConfirmPassword")]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [System.ComponentModel.DataAnnotations.Compare("Password")]
        [Display(Name = "Confirm password")]
        public string ConfirmPassword { get; set; }
        
        [Display(Name = "Login")]
        [Required]
        [StringLength(256)]
        [Remote("Validate", "Account", AdditionalFields = "Email,UserId", ErrorMessage = "User with the same email or login already exists.")]
        public string Login { get; set; }

        [UIHint("Upload")]
        [Required]
        public string Avatar { get; set; }

        public static User operator +(User user, EditProfileModel model)
        {
            ToEntity(model, user);
            if (!string.IsNullOrWhiteSpace(model.Password) && model.Password.Equals(model.ConfirmPassword))
                user.SetPassword(model.Password);
            return user;
        }

    }
}